//Installer namespace
var Installer = (function( ){
	//MircoAjax library
	//Found at: http://code.google.com/p/microajax/
	//MicroAjax is under GPL software license
	var _microAjax = function(B,A){this.bindFunction=function(E,D){return function(){return E.apply(D,[D])}};this.stateChange=function(D){if(this.request.readyState==4){this.callbackFunction(this.request.responseText)}};this.getRequest=function(){if(window.ActiveXObject){return new ActiveXObject("Microsoft.XMLHTTP")}else{if(window.XMLHttpRequest){return new XMLHttpRequest()}}return false};this.postBody=(arguments[2]||"");this.callbackFunction=A;this.url=B;this.request=this.getRequest();if(this.request){var C=this.request;C.onreadystatechange=this.bindFunction(this.stateChange,this);if(this.postBody!==""){C.open("POST",B,true);C.setRequestHeader("X-Requested-With","XMLHttpRequest");C.setRequestHeader("Content-type","application/x-www-form-urlencoded");C.setRequestHeader("Connection","close")}else{C.open("GET",B,true)}C.send(this.postBody)}};
	
	//Template pattern del title
	var _titlePattern = document.title;
	//DOM pointers
	var _fieldsWrapper = document.getElementById( 'stepFields' );
	var _proceedButton = document.getElementById( 'proceed' );
	
	//Submittal function
	var _proceed = (function( success ){
		//Querystring params building
		var _params = (function( ){
			var _res = '?stepNumber=' + document.getElementById( 'stepNumber' ).value;
			var _forwardFields = _fieldsWrapper.getElementsByTagName( 'input' );
			for( var i in _forwardFields ){
				if( _forwardFields[i].className == 'forward' ){
					_res += '&' + _forwardFields[i].id + '=' + _forwardFields[i].value;
				}
			}
			return _res;
		})();
		_microAjax( 'install.php' + _params, (function( response ){
			var _response = eval( "(" + response + ")" );
			if( _response.status == 'ok' ){
				alert( _response.msg );
				success( );
				return;
			}
			alert( _response.msg );
			return;
		}));
	});

	//Step functions
	var _dbInstall = (function( success ){
		//Form markup definition
		_fieldsWrapper.innerHTML =
		'<label for="host">Host</label><input class="forward" id="host" type="text" value="localhost"/>'+
		'<br/>'+
		'<label for="port">Port</label><input style="width: 50px;" class="forward" id="port" type="text" value="3306"/>'+
		'<br/>'+
		'<label for="schema">Schema</label><input class="forward" id="schema" type="text" value=""/>'+
		'<br/>'+
		'<label for="user">Username</label><input class="forward" id="user" type="text" value=""/>'+
		'<br/>'+
		'<label for="password">Password</label><input class="forward" id="password" type="password" value=""/>'
		;
		//Proceed button success definition
		_proceedButton.onclick = (function( ){
			_proceed(  success );
		});
	});
	
	var _installationSuccess=(function( success ){
		//Markup definition
		_fieldsWrapper.innerHTML =
		'<br/>'+
		'<h2>Thanks!</h2>'+
		'<p>Now you can start configure your system</p>';
		//Proceed button success definition
		_proceedButton.onclick = (function( ){
			_proceed(  success );
		});
	});
	
	var _cdnConfig =(function( success ){
		//Form markup definition
		_fieldsWrapper.innerHTML =
		'<label for="use_cdn">Use CDN</label><input id="use_cdn" class="forward" value="yes" type="checkbox" checked/>'+
		'<br/>'+
		'<label for="base_url">CDN Base URL</label><input style="width: 320px;" class="forward" id="base_url" type="text" value="http://"/>';
		//Cdn checkbox behavior
		document.getElementById( 'use_cdn' ).onclick = (function( ){
			if( !this.checked ){
				document.getElementById( 'base_url' ).disabled = true;
			}
			else{
				document.getElementById( 'base_url' ).removeAttribute( 'disabled' );
			}
		});
		//Proceed button success definition
		_proceedButton.onclick = (function( ){
			_proceed( success );
		});
	});
	
	var _adminCreation=(function( success ){
		//Form markup definition
		_fieldsWrapper.innerHTML =
		'<label for="username">Username</label><input id="username" class="forward" type="input"/>'+
		'<br/>'+
		'<label for="password">Password</label><input style="width: 320px;" class="forward" id="password" type="password"/>';
		//Proceed button success definition
		_proceedButton.onclick = (function( ){
			_proceed( success );
		});
	});

	//Steps
	var _step = document.getElementById( 'stepNumber' ).value -1;
	var _steps = [];
	_steps[0] = {number:'1',label:'Database Installation',func:_dbInstall};
	_steps[1] = {number:'2',label:'CDN Configuration',func:_cdnConfig};
	_steps[2] = {number:'3',label:'Administrator acount',func:_adminCreation};
	_steps[3] = {number:'4',label:'Software successfully installed',func:_installationSuccess};
	
	var _exec = (function( ){
		document.title = ( _titlePattern.replace( '{#step_number}' ,_steps[_step]['number'] ).replace( '{#step_name}', _steps[_step]['label'] ) );
		document.getElementById( 'stepTitle' ).innerHTML =  _steps[_step]['label'];
		document.getElementById( 'stepNumber' ).value = _steps[_step]['number'];
		var _f = _steps[_step]['func'];
		//Success callback
		var _success = (function(){
			if( _step < ( _steps.length -1 )){
				_step++;
				_exec( );
			}
			//Last step, index redirect
			else{
				document.location = 'index.php';
			}
		});
		_f( _success );
	});	
	_exec( );
	
})( );